package com.syt.suan.common.core.domain;

import com.syt.suan.common.core.enums.ResultCode;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import lombok.experimental.Accessors;

@Setter(AccessLevel.PRIVATE)
public class R<D> {
    /**
     * 消息状态码
     */
    private int code;
    /**
     * 消息内容
     */
    private String msg;
    /**
     * 数据对象
     */
    private D data;

    private R() {
    }

    public static <T> R<T> success() {
        R<T> r = new R<>();
        r.setCode(ResultCode.SUCCESS.getCode());
        r.setMsg(ResultCode.SUCCESS.getMsg());
        return r;
    }

    public static <T> R<T> failed() {
        R<T> r = new R<>();
        r.setCode(ResultCode.FAILED.getCode());
        r.setMsg(ResultCode.FAILED.getMsg());
        return r;
    }

    public R<D> code(ResultCode code) {
        this.setCode(code.getCode());
        this.setMsg(code.getMsg());
        return this;
    }

    public R<D> code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R<D> msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public R<D> data(D data) {
        this.setData(data);
        return this;
    }
}